package com.test.mytest.jmx;

import javax.management.Attribute;
import javax.management.MBeanServerConnection;
import javax.management.MBeanServerInvocationHandler;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;

public class Client {
    public static void main(String[] args) throws Exception {
        JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:9999/jmxrmi");
        JMXConnector jmxc = JMXConnectorFactory.connect(url,null);
        MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
        ObjectName mbeanName = new ObjectName("jmxBean:name=hello");
        System.out.println("Domains .......");
        String [] domains = mbsc.getDomains();
        for(int i=0;i<domains.length;i++){
            System.out.println("domain["+i+"]="+domains[i]);
        }

        System.out.println("MBean count = "+mbsc.getMBeanCount());
        mbsc.setAttribute(mbeanName,new Attribute("Name","杭州"));
        mbsc.setAttribute(mbeanName,new Attribute("Age","1990"));
        String age = (String)mbsc.getAttribute(mbeanName,"Age");
        String name = (String)mbsc.getAttribute(mbeanName,"Name");
        System.out.println("age="+age+";name="+name);
        HelloMBean proxy = MBeanServerInvocationHandler.newProxyInstance(mbsc,mbeanName,HelloMBean.class,false);
        proxy.helloWorld();
        proxy.helloWorld("migu");
        proxy.getTelephone();
        mbsc.invoke(mbeanName,"getTelephone",null,null);
        mbsc.invoke(mbeanName,"helloWorld",new String[]{"I'll connect to JMX Server via client"},new String[]{"java.lang.String"});

        mbsc.invoke(mbeanName,"helloWorld",null,null);
    }
}
